import { Token } from "@/types/data"
import { Channel, History } from "@/types/store"
const TOKEN_KEY = 'TOKEN_INFO'

export const getToken = (): Token => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY)!)
}
// export const getToken = () => {
//   return JSON.parse(<string>localStorage.getItem(token)) || null
// }
// export const getToken = () => {
//   return JSON.parse(localStorage.getItem(token) as string) || null
// }

export const setToken = (value: Token): void => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(value))
}

export const clearToken = () => {
  localStorage.removeItem(TOKEN_KEY)
}

export function hasToken(): boolean {
  return !!getToken()
}


const CHANNEL_KEY = 'CHANNEEL_INFO'
/**
 * 保存频道数据
 * @param channels
 */
export function setChannels(channels: Channel[]): void {
  localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels))
}

/**
 * 获取频道列表数据
 * @returns
 */
export function getChannels(): Channel[] {
  return JSON.parse(localStorage.getItem(CHANNEL_KEY) || '[]')
}



// 搜索关键字的本地缓存键名
const SEARCH_HIS_KEY = 'geek-h5-sh88-channel'

/**
 * 从缓存获取搜索历史关键字
 */
export const getLocalHistories = (): History => {
  return JSON.parse(localStorage.getItem(SEARCH_HIS_KEY) || '[]')
}

/**
 * 将搜索历史关键字存入本地缓存
 * @param {Array} histories
 */
export const setLocalHistories = (histories: History): void => {
  localStorage.setItem(SEARCH_HIS_KEY, JSON.stringify(histories))
}

/**
 * 删除本地缓存中的搜索历史关键字
 */
export const removeLocalHistories = () => {
  localStorage.removeItem(SEARCH_HIS_KEY)
}
